package Timing;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * Created by LeeXyan on 2017/03/27 10:33
 */
public class Timing {

    /**
     * 将时间戳转换为时间
     */
    public static String stampToDate(String s) {
        long lt = new Long(s);
        Date date = new Date(lt * 1000);
        Calendar cld = Calendar.getInstance();
        cld.setTime(date);
        cld.add(Calendar.DATE, 9);
        cld.add(Calendar.MONTH, 7);
        cld.add(Calendar.YEAR, 40);
        return DateToStr(cld.getTime(), "yyyy-MM-dd HH:mm:ss");
    }

    public static Date StrToDate(String date, String f) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat(f);
        return format.parse(date);
    }

    /*
     * 将时间转换为时间戳
     */
    public static String dateToStamp(String s) throws ParseException {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        res = String.valueOf(ts / 1000);
        return res;
    }

    public static String DateToStr(Date date, String f) {
        SimpleDateFormat format = new SimpleDateFormat(f);
        if (date == null) {//防止没有时间
            return format.format(new Date());
        }
        return format.format(date);
    }

    public static String addString(String time) {
        Date date = null;
        Calendar cld = Calendar.getInstance();
        try {
            cld.setTime(StrToDate(time, "yyyy-MM-dd HH:mm:ss"));
            cld.add(Calendar.DATE, -9);
            cld.add(Calendar.MONTH, -7);
            cld.add(Calendar.YEAR, -40);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return DateToStr(cld.getTime(), "yyyy-MM-dd HH:mm:ss");
    }

    public static String reduceString(String time) {
        Date date = null;
        Calendar cld = Calendar.getInstance();
        try {
            cld.setTime(StrToDate(time, "yyyy-MM-dd HH:mm:ss"));
            cld.add(Calendar.DATE, 9);
            cld.add(Calendar.MONTH, 7);
            cld.add(Calendar.YEAR, 40);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return DateToStr(cld.getTime(), "yyyy-MM-dd");
    }

    public static void main(String[] args) throws ParseException {
        String time = "2017-07-04";

        System.out.println("2017-07-05:" + dateToStamp(addString("2017-07-05 00:00:00")));
        System.out.println("2017-07-06:" + dateToStamp(addString("2017-07-06 00:00:00")));


        System.out.println("217786423:" + stampToDate("217786423"));
        System.out.println(dateToStamp("2017-07-04 00:00:00"));
        System.out.println(dateToStamp("2017-07-05 00:00:00"));



        SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        /*String time1="1970-01-06 11:45:55";
        Date date = format.parse(time1);
        System.out.println("Format To times:"+date.getTime());


        SimpleDateFormat format2 =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Long time2=new Long(445555000);
        String d = format2.format(time2);
        Date date2=format2.parse(d);
        System.out.println("Format To String(Date):"+d);
        System.out.println("Format To Date:"+date2);*/




        String time3="2017-01-06 11:45:55";
        Date date3 = format.parse(time3);
        System.out.println("2017-01-06 11:45:55 "+date3.getTime());



       /* Long time4=new Long(217932124000L);*/
        Integer time4=new Integer(217932124);
        String d4 = format.format(time4);
        System.out.println("Format To String(Date):"+d4);

    }


}
